package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

// 登录成功后 跳转到的页面.
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 首页这里也要先获取 session, 此处的 session 正是在登录成功时创建出来的
        // 此处的参数写为 false 即可. 表示不新建, 不存在就返回 null .
        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.setStatus(403);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("您尚未登陆,不能访问主页!");
            return;
        }
        String username = (String) session.getAttribute("username");
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("欢迎来到主页: " + username);
    }
}
